package com.ahyesyesteam.aicarfaultrepair.service.impl;

import com.ahyesyesteam.aicarfaultrepair.pojo.User;
import com.ahyesyesteam.aicarfaultrepair.service.TokenService;
import com.ahyesyesteam.aicarfaultrepair.util.JWTUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class TokenServiceImpl implements TokenService {
    @Resource
    RedisTemplate<String, String> redisTemplate;

    @Override
    public User checkToken(String token) {
        if(!StringUtils.hasText(token)){
            return null;
        }

        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if(stringObjectMap == null){
            return null;
        }

//        String userJson = redisTemplate.opsForValue().get("TOKEN_"+token);
//        if(!StringUtils.hasText(userJson)){
//            return null;
//        }
        User user = new User();
        user.setId((Long) stringObjectMap.get("id"));
        user.setAccount((String) stringObjectMap.get("account"));
        user.setNickname((String) stringObjectMap.get("nickname"));

        return user;
    }
}
